<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Groups extends Controller
{
    //角色列表
    public function index(Request $request)
    {
        //实现分页
        $data['page'] = (int)$request->page;//获取分页
        $data['pageSize'] = 10;
        $pages = DB::table('admin_group')->pages($data['pageSize']);
        $data['groups'] = $pages['lists'];
        $data['total']        = $pages['total']; //总数
        return view('admin.groups.index',$data);
    }
    public function add()
    {
        $menu = DB::table('admin_menu')->where('status',0)->cates('mid');
        $menus = $this->gettreeitems($menu);
        $results = array();
        foreach ($menus as $value) {
            $value['children'] = isset($value['children'])?$this->formateMenus($value['children']):false;
            $results[] = $value;
        }
//        echo '<pre>';
//        print_r($results);
//        print_r($data['role']['rights']);exit();
        $data['menus'] = $results;
//        echo '<pre>';
//        print_r($data['menus']);exit();
        return view('admin.groups.add',$data);
    }
    public function edit(Request $request)
    {
        $gid = (int)$request->gid;
        $data['role'] = DB::table('admin_group')->where(array('gid'=>$gid))->item();

        if($data['role']['rights']){
            $data['role']['rights'] = json_decode($data['role']['rights']);
        }

        $menu_list = DB::table('admin_menu')->where('status',0)->cates('mid');
        $menus = $this->gettreeitems($menu_list);
        $results = array();
        foreach ($menus as $value) {
            $value['children'] = isset($value['children'])?$this->formateMenus($value['children']):false;
            $results[] = $value;
        }
//        echo '<pre>';
//        print_r($results);
//        print_r($data['role']['rights']);exit();
        $data['menus'] = $results;
//        echo '<pre>';
//        print_r($data['menus']);exit();
        return view('admin.groups.edit',$data);
    }
    public function save(Request $request)
    {
        $gid = (int)$request->gid;
        $data['title'] = trim($request->title);
        $data['desc'] = trim($request->desc);
        $menus = $request->menus;
        $menus = array_keys($menus);

        $data['rights'] = json_encode($menus);

        if($gid==0){
            DB::table('admin_group')->insert($data);
        }else{
            DB::table('admin_group')->where('gid',$gid)->update($data);
        }

        exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
    }

    // 获取树型菜单的元素
    private function gettreeitems($items){
        $tree = array();
        foreach ($items as $item){
            if (isset($items[$item['pid']])){
                $items[$item['pid']]['children'][] = &$items[$item['mid']];
            }else{
                $tree[] = &$items[$item['mid']];
            }
        }
        return $tree;
    }

    // 处理角色菜单
    private function formateMenus($items,&$res = array()){
        foreach ($items as $item) {
            if(!isset($item['children'])){
                $res[] = $item;
            }else{
                $tem = $item['children'];
                unset($item['children']);
                $res[] = $item;
                $this->formateMenus($tem,$res);
            }
        }
        return $res;
    }
}
